package com.example.project.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

import com.example.project.entity.User;
import com.example.project.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.project.entity.Comment;
import com.example.project.mapper.CommentMapper;

/**
 * 评论管理Controller
 *
 * @author changyao
 * @date 2024-10-01
 */
@RestController
@RequestMapping("/project/comment")
public class CommentController
{
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询评论管理列表
     */
    @GetMapping("/list")
    public List<Comment> list()
    {
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        List<Comment> comments = commentMapper.selectList(queryWrapper);
        for (Comment comment : comments) {
            Long userId = comment.getUserId();
            User user = userMapper.selectById(userId);
            comment.setUserinfo(user);

        }


        return comments;
    }




    /**
     * 获取评论管理详细信息
     */
    @PostMapping("/detil")
    public Comment getInfo(@RequestBody Comment comment)
    {
        return commentMapper.selectById(comment);
    }


    /**
     * 新增评论管理
     */
    @PostMapping("/add")
    public int add(@RequestBody Comment comment)
    {
        comment.setRootId(comment.getPid());
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = currentTime.format(formatter);
        comment.setTime(formattedTime);
        return commentMapper.insert(comment);
    }
    /**
     * 修改评论管理
     */
    @PostMapping("/edit")
    public int edit(@RequestBody Comment comment)
    {
        return commentMapper.updateById(comment);
    }

    /**
     * 删除评论管理
     */
    @PostMapping("/del")
    public int remove(@RequestBody Comment comment)
    {
        return commentMapper.deleteById(comment);
    }

    @GetMapping("/selectTree/{fid}/{module}")
    public List<Comment> selectTree(@PathVariable Integer fid, @PathVariable String module)
    {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fid", fid).eq("module", module).orderByDesc("id");

        List<Comment> commentList  = commentMapper.selectList(queryWrapper);
        for (Comment root : commentList) {
            root.setUserinfo(userMapper.selectById(root.getUserId()));
        }

        // 筛选出最外层的节点
        List<Comment> rootList = commentList.stream().filter(comment -> comment.getPid() == null).collect(Collectors.toList());
        for (Comment root : rootList) {
            Long rootId = root.getId();
            List<Comment> children = commentList.stream().filter(comment -> rootId.equals(comment.getRootId())).collect(Collectors.toList());
            System.out.println(children);
            root.setChildren(children);
        }
        return rootList;
    }
}
